package com.yserp.project.erp.basic.reservoir.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.basic.reservoir.domain.BasisWarehouseReservoir;
import com.yserp.project.erp.basic.reservoir.service.IBasisWarehouseReservoirService;
import com.yserp.project.erp.basic.warehouse.domain.BasisWarehouse;
import com.yserp.project.erp.basic.warehouse.service.IBasisWarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 仓库库区Controller
 *
 * @author yserp
 * @date 2022-07-06
 */
@Controller
@RequestMapping("/basic/reservoir")
public class BasisWarehouseReservoirController extends BaseController {
    private String prefix = "erp/basic/reservoir";

    @Autowired
    private IBasisWarehouseReservoirService basisWarehouseReservoirService;
    @Autowired
    private IBasisWarehouseService warehouseService;

    @RequiresPermissions("basic:reservoir:view")
    @GetMapping()
    public String reservoir(ModelMap mmap) {
        List<BasisWarehouse> basisWarehouses = warehouseService.selectBasisWarehouseList(new BasisWarehouse());
        mmap.put("warehouseList",basisWarehouses);
        return prefix + "/reservoir";
    }

    /**
     * 查询库区设置列表
     */
    @RequiresPermissions("basic:reservoir:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasisWarehouseReservoir basisWarehouseReservoir) {
        startPage();
        List<BasisWarehouseReservoir> list = basisWarehouseReservoirService.selectBasisWarehouseReservoirList(basisWarehouseReservoir);
        return getDataTable(list);
    }

    /**
     * 导出库区设置列表
     */
    @RequiresPermissions("basic:reservoir:export")
    @Log(title = "库区设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasisWarehouseReservoir basisWarehouseReservoir) {
        List<BasisWarehouseReservoir> list = basisWarehouseReservoirService.selectBasisWarehouseReservoirList(basisWarehouseReservoir);
        ExcelUtil<BasisWarehouseReservoir> util = new ExcelUtil<BasisWarehouseReservoir>(BasisWarehouseReservoir.class);
        return util.exportExcel(list, "库区设置数据");
    }

    /**
     * 新增库区设置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存库区设置
     */
    @RequiresPermissions("basic:reservoir:add")
    @Log(title = "库区设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasisWarehouseReservoir basisWarehouseReservoir) {
        return toAjax(basisWarehouseReservoirService.insertBasisWarehouseReservoir(basisWarehouseReservoir));
    }

    /**
     * 修改库区设置
     */
    @RequiresPermissions("basic:reservoir:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BasisWarehouseReservoir basisWarehouseReservoir = basisWarehouseReservoirService.selectBasisWarehouseReservoirById(id);
        mmap.put("basisWarehouseReservoir", basisWarehouseReservoir);
        return prefix + "/edit";
    }

    /**
     * 修改保存库区设置
     */
    @RequiresPermissions("basic:reservoir:edit")
    @Log(title = "库区设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasisWarehouseReservoir basisWarehouseReservoir) {
        return toAjax(basisWarehouseReservoirService.updateBasisWarehouseReservoir(basisWarehouseReservoir));
    }

    /**
     * 删除库区设置
     */
    @RequiresPermissions("basic:reservoir:remove")
    @Log(title = "库区设置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(basisWarehouseReservoirService.deleteBasisWarehouseReservoirByIds(ids));
    }

    /**
     * 校验库区编码
     */
    @PostMapping("/checkReservoirCodeUnique")
    @ResponseBody
    public String checkReservoirCodeUnique(BasisWarehouseReservoir basisWarehouseReservoir) {
        return basisWarehouseReservoirService.checkReservoirCodeUnique(basisWarehouseReservoir.getReservoirCode());
    }

    /**
     * ajax查询仓库库区列表
     */
    @PostMapping("/ajaxList")
    @ResponseBody
    public AjaxResult ajaxList(BasisWarehouseReservoir basisWarehouseReservoir) {
        List<BasisWarehouseReservoir> list = basisWarehouseReservoirService.selectBasisWarehouseReservoirList(basisWarehouseReservoir);
        return AjaxResult.success(list);
    }
}
